// 读取数据
const getData = (url, obj) => {
    axios.get(url).then((res) => {
        obj.total = res.data.total;
        obj.pagesize = res.data['per_page'];
        obj.currentpage = res.data['current_page'];
        obj.tableData = res.data.data;

    })
}
//分页读取
const getDataByPage = (url, idx, obj) => {
    axios.get(url + '?page=' + idx).then((res) => {
        obj.total = res.data.total;
        obj.pagesize = res.data['per_page'];
        obj.tableData = res.data.data;
    })
}
//编辑
const editData = (url, id, obj) => {
    axios({
        url: url + '/' + id + '/edit',
        method: 'get'
    }).then((res) => {
        obj.formdata.id = res.data['id'];
        obj.formdata.name = res.data['name'];
        obj.formdata.email = res.data['email'];
        obj.submitUrl = url +'/'+ id;
        obj.submitMethod = 'put';
        obj.dialogVisible = true;
    })
}
//删除
const delData = (url, id, obj) => {
    obj.$confirm('确认删除该用户吗?', '提示', {
        type: 'warning'
    }).then(() => {
        window.axios({
            url: url + '/' + id,
            method: 'delete',
        }).then((res) => {
            obj.$message({
                showClose: true,
                type: 'success',
                message: res.data
            })
            getDataByPage(url, 1, obj);
            obj.currentpage = 1;
        })
    });
}
//批量删除
const batchRm = (url, obj) => {
    var ids = obj.sels.map(item => item.id).toString();
    obj.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
    }).then(() => {
        let para = { ids: ids };
        window.axios({
            url: url + '/batch',
            method: 'post',
            data: para
        }).then((res) => {
            obj.$message(res.data);
            getData(url, obj);
            obj.currentpage = 1;
        })
    }).catch(() => {
        console.log('失败');
    });
}
export default {
    getData,
    getDataByPage,
    editData,
    delData,
    batchRm
}